package com.aidex.web.controller.biz;

import com.aidex.biz.domain.OrderImage;
import com.aidex.biz.service.OrderImageService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单详情照片Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-09-11
 */
@RestController
@RequestMapping("/biz/bizOrderImage")
public class OrderImageController extends BaseController
{
    @Autowired
    private OrderImageService orderImageService;

    /**
     * 查询订单详情照片列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderImage:list')")
    @GetMapping("/list")
    public R<PageInfo> list(OrderImage orderImage, HttpServletRequest request, HttpServletResponse response)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            orderImage.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
         }
        orderImage.setPage(new PageDomain(request, response));
        return R.data(orderImageService.findPage(orderImage));
    }

    /**
     * 获取订单详情照片详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderImage:query')")
    @GetMapping(value = "/{id}")
    public R<OrderImage> detail(@PathVariable("id") String id)
    {
        return R.data(orderImageService.get(id));
    }

    /**
     * 新增订单详情照片
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderImage:add')")
    @Log(title = "订单详情照片", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  OrderImage orderImage)
    {
        return R.status(orderImageService.save(orderImage));
    }

    /**
     * 修改订单详情照片
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderImage:edit')")
    @Log(title = "订单详情照片", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated OrderImage orderImage)
    {
        return R.status(orderImageService.save(orderImage));
    }


    /**
     * 删除订单详情照片
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderImage:remove')")
    @Log(title = "订单详情照片", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(orderImageService.deleteOrderImageByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderImage:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(orderImageService.findMaxSort(new OrderImage()));
    }

    /**
     * 导出订单详情照片列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderImage:export')")
    @Log(title = "订单详情照片", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(OrderImage orderImage)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    orderImage.setCompanyId(null);
            }else{
                         orderImage.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<OrderImage> list = orderImageService.findList(orderImage);
        ExcelUtil<OrderImage> util = new ExcelUtil<OrderImage>(OrderImage.class);
        return util.exportExcel(list, "订单详情照片数据");
    }

}
